<?php
/*
 * Creative Bulletin Board Generation 3
 * Version 1.0 Beta 1
 +------------------------------------
 * Core Database Class + Interface
 * Started: August 22, 2009 11:02:52 AM
 * Script version: 1.0.2
 */

interface database{
	public function query($query);
	public function fetch_array($resource);
	public function num_rows($resource);
	public function num_fields($resource);
	public function conf_value($key);
	public function select($table, $key = "*", $limit = 0, $extra2 = "");
}

class mysql implements database{
	public function __construct(){ # connect
		global $config;
		$this->queries = 0;
		$this->queries_run = "";
		if(isset($config)){
			$this->conn = mysql_pconnect($config["db"]["host"], $config["db"]["username"], $config["db"]["password"]);
			if(!$this->conn){
				trigger_error("Failed to connect to MySQL Database");
			}
			$this->seldb = mysql_select_db($config["db"]["db"]);
			if(!$this->seldb){
				trigger_error("Couldn't select database");
			}
		}
		else{
			trigger_error("Couldn't initialize configuration value");
		}
	}
	
	public function query($query){
		// TODO: Cache the query results
		$this->queries++;
		$this->queries_run .= $query."\n";
		return mysql_query($query);
	}
	
	public function fetch_array($resource){
		return mysql_fetch_array($resource);
	}
	
	public function num_rows($resource){
		return mysql_num_rows($resource);
	}
	
	public function num_fields($resource){
		return mysql_num_fields($resource);
	}
	
	public function conf_value($key){
		// Lets make it clear. We are gonna do it in a different way. This is way too resource incentive...
		if(!isset($this->conf_array)){
			$query = $this->select("config");
			if(!$query){
				trigger_error("Configuration fetch failed, couldn't find table");
			}
			elseif($this->num_rows($query) === 0){
				trigger_error("Couldn't find any configuration value in config table");
			}
			while($array = $this->fetch_array($query)){
				$this->conf_array[$array['key']] = $array['value'];
			}
		}
		if(isset($this->conf_array[$key])){
			return $this->conf_array[$key];
		}
		else{
			trigger_error("Couldn't find configuration value {$key} in configuration table/array.");
		}
	}
	
	public function user_conf_value($uid, $key, $fallback = false){
		$uid = intval($uid);
		$key = mysql_real_escape_string(stripslashes($key));
		if($fallback) $fallback = mysql_real_escape_string(stripslashes($fallback));
		$query = $this->query("SELECT SQL_CACHE * FROM `".DB_PREFIX."user_config` WHERE `uid` = {$uid} AND `key` = '{$key}' LIMIT 1");
		if(!$this->num_rows($query)){
			if($fallback){
				return $this->conf_value($fallback);
			}
			else{
				trigger_error("Couldn't find user-specific configuration value {$key} by {$uid} and no fallback was provided");
			}
		}
		else{
			$array = $this->fetch_array($query);
			return $array['value'];
		}
	}
	
	public function select($table, $key = "*", $limit = 0, $extra2 = ""){
		$extra = ($limit != 0 ? "LIMIT {$limit}" : "");
		$prepared = trim("SELECT SQL_CACHE {$key} FROM `".DB_PREFIX.$table."` {$extra2} {$extra}");
		return $this->query($prepared);
	}
}

if(class_exists($config["db"]["engine"])){
	eval("class db extends {$config["db"]["engine"]} implements database{}"); // Implement
}
